/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLLER;

import POJO.*;
import BUS.*;
import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 *
 * @author Mr Long
 */
@Controller
public class SanphamController {

    private SanphamBUS spBUS;
    private LoaisanphamBUS lspBUS;
    private HinhanhBUS haBUS;

    //Xử lý hiển thị danh sách sản phẩm
    @RequestMapping(value = "/quanlisanpham")
    public ModelAndView sanphamShow(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            mv.put("lstSP", spBUS.dsSanphamAll());

        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("quanlisanphamview", mv);
    }
    //Xử lý xóa sản phẩm

    @RequestMapping(value = "/xoasanpham", method = RequestMethod.GET)
    public ModelAndView xoaSpAction(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            String masp = request.getParameter("MaSP");
            spBUS.xoaSanpham(masp);
            mv.put("lstSP", spBUS.dsSanphamFi(1));
        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("quanlisanphamview", mv);
    }
    //Xử lý cập nhật sản phẩm

    @RequestMapping(value = "/capnhatsanpham", method = RequestMethod.GET)
    public ModelAndView capnhatSpAction(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> model = new HashMap<String, Object>();
        //Do something...
        try {
            Sanpham sp = spBUS.laySanpham(request.getParameter("MaSP"));
            model.put("sanPham", sp);
            //Lấy danh sách danh mục sản phẩm            
            List list = lspBUS.dsLoaisanphamAll();
            model.put("lstLSP", list);
            //Lấy danh sách hinh anh

        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("capnhatsanphamview", model);
    }
    //Xử lý hiển thị form thêm sản phẩm

    @RequestMapping(value = "/tiepnhansanpham")
    public ModelAndView tiepnhansanphamShow(HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            mv.put("sanPham", new Sanpham());
            //Lấy danh sách danh mục sản phẩm            
            mv.put("lstLSP", lspBUS.dsLoaisanphamAll());

        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("capnhatsanphamview", mv);
    }

    @RequestMapping(value = "/quanlihinhanh")
    public ModelAndView hinhsanphamShow(HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            String maSP = request.getParameter("MaSP");
            Sanpham sp = spBUS.laySanpham(maSP);
            List lstHA = haBUS.dsHinhSanPham(maSP);
            mv.put("sanPham", sp);
            //Lấy danh sách danh mục sản phẩm            
            mv.put("lstHA", lstHA);

        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("quanlihinhanhview", mv);
    }

    @RequestMapping(value = "/xoahinhanh", method = RequestMethod.GET)
    public ModelAndView xoahinhAnhAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            String maHA = request.getParameter("MaHA");
            String maSP = request.getParameter("MaSP");
            haBUS.deleteHinhanh(maHA);
            Sanpham sp = spBUS.laySanpham(maSP);
            List lstHA = haBUS.dsHinhSanPham(maSP);
            mv.put("sanPham", sp);
            mv.put("lstHA", lstHA);

        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("quanlihinhanhview", mv);
    }

    //Xử lý thêm/cập nhật sản phẩm
    @RequestMapping(value = "submitsanpham", method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("sanPham") Sanpham _sp, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=UTF-8");

        try {
            if (_sp != null) {
                spBUS.luuSanpham(_sp);
            }

        } catch (Exception ex) {
            System.err.print(ex);
        }
        return "redirect:/quanlisanpham.html";

    }

    @RequestMapping(value = "/themhinhsanpham")
    public ModelAndView themhinhAnhAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            String maSP = request.getParameter("MaSP");
            Sanpham sp = spBUS.laySanpham(maSP);
            mv.put("sanPham", sp);
        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("themhinhanhview", mv);
    }

    @RequestMapping(value = "/submitthemhinhsanpham", method = RequestMethod.POST)
    public ModelAndView submitthemhinhAnhAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> mv = new HashMap<String, Object>();
        //Do something...
        try {
            boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
            if (!isMultiPart) {
            } else {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List items = new ArrayList();
                try {
                    items = upload.parseRequest(request);
                } catch (FileUploadException e) {
                }
                Iterator iter = items.iterator();
                Hashtable params = new Hashtable();
                String[] dsFile = new String[]{"", "", "", "", "", ""};
                String fileName = "";
                int n = 0;
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    if (item.isFormField()) {
                        params.put(item.getFieldName(), item.getString());
                    } else {
                        try {
                            String itemName = item.getName();
                            fileName = itemName.substring(itemName.lastIndexOf("\\") + 1);
                            String RealPath = request.getRealPath("") + "/image/" + fileName;
                            File savedFile = new File(RealPath);
                            item.write(savedFile);
                            dsFile[n++] = "image/" + fileName;
                        } catch (Exception ex) {
                        }
                    }
                }
                String maSP = (String) params.get("MaSP");
                for (int i = 0; i < n; i++) {
                    Hinhanh ha = new Hinhanh();
                    ha.setMaSanPham(maSP);
                    ha.setDuongDan(dsFile[i]);
                    haBUS.saveHinhanh(ha);
                }
                Sanpham sp = spBUS.laySanpham(maSP);
                List lstHA = haBUS.dsHinhSanPham(maSP);
                mv.put("sanPham", sp);
                mv.put("lstHA", lstHA);
            }
        } catch (Exception ex) {
            System.err.print(ex);
        }
        return new ModelAndView("quanlihinhanhview", mv);
    }

    /**
     * @param spBUS the spBUS to set
     */
    @Autowired
    public void setSpBUS(SanphamBUS spBUS) {
        this.spBUS = spBUS;
    }

    /**
     * @param lspBUS the lspBUS to set
     */
    @Autowired
    public void setLspBUS(LoaisanphamBUS lspBUS) {
        this.lspBUS = lspBUS;
    }

    /**
     * @param haBUS the haBUS to set
     */
    @Autowired
    public void setHaBUS(HinhanhBUS haBUS) {
        this.haBUS = haBUS;
    }
}
